<template>
  <el-dialog :visible="dialogFormVisible" :title="textMap[dialogStatus]" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" :disabled="isView"/>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname" :disabled="isView"/>
      </el-form-item>
      <el-form-item v-if="dialogStatus == 'create'" label="密码" prop="password" >
        <el-input v-model="form.password" :disabled="isView" type="password"/>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" :disabled="isView"/>
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select v-model="form.roles" multiple placeholder="请选择角色">
          <el-option
            v-for="item in roles"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="user_type">
        <el-select v-model="form.user_type" :disabled="isView" placeholder="请选择用户类型">
          <el-option label="系统服务用户" value="1"/>
          <el-option label="内部人员" value="2"/>
          <el-option label="客户" value="3"/>
          <el-option label="兼职" value="4"/>
        </el-select>
      </el-form-item>
      <el-form-item label="有效期" prop="expiry_date">
        <el-date-picker
          v-if="isView"
          :editable="false"
          v-model="form.expiry_date"
          disabled
          type="date"
          placeholder="有效期"
        />
        <el-date-picker
          v-else
          :editable="false"
          v-model="form.expiry_date"
          type="date"
          placeholder="有效期"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" :disabled="isView" type="textarea"/>
      </el-form-item>
    </el-form>
    <div v-if="!isView" slot="footer" cslass="dialog-footer">
      <el-button v-waves @click="close">取 消</el-button>
      <el-button v-waves @click="reset">重 置</el-button>
      <el-button v-waves :loading="submitFormLoading" type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, update } from '@/api/user'
export default {
  name: 'UserEdit',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
      required: true
    },
    form: {
      type: Object,
      default: () => {
        return {
          id: null,
          name: null,
          nickname: null,
          password: null,
          mobile: null,
          roles: null,
          user_type: null,
          expiry_date: null,
          remark: null
        }
      }
    },
    isView: {
      type: Boolean,
      default: false
    }
  },
  data() {
    var validateMobile = (rule, value, callback) => {
      const re = /^1\d{10}$/
      if (value === '') {
        callback(new Error('请输入手机号码'))
      } else if (!re.test(value)) {
        callback(new Error('手机号码格式有误'))
      } else {
        callback()
      }
    }
    return {
      selectedOptions: [],
      textMap: {
        view: '查看',
        update: '编辑',
        create: '新增'
      },
      submitFormLoading: false,
      roles: [{ value: 1, label: '管理员' }, { value: 2, label: '部门经理' }, { value: 3, label: 'COO' }],
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        nickname: [
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        mobile: [
          { validator: validateMobile, trigger: 'blur' }
        ],
        user_type: [
          { required: true, message: '请选择用户类型', trigger: 'change' }
        ],
        expiry_date: [
          { type: 'date', required: true, message: '请选择有效期', trigger: 'change' }
        ],
        remark: [
          { required: false, message: '请填写备注', trigger: 'blur' },
          { max: 500, message: '只能最多输入 500 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset() {
      this.submitFormLoading = false
      this.$emit('reset', this.form.id)
    },
    submitForm() {
      this.submitFormLoading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            update(this.form).then(response => {
              this.submitFormLoading = false
              // 关闭弹框，刷新列表
              this.$emit('closeDialogAndRefresh')
            }).catch(() => {
              this.submitFormLoading = false
            })
          } else {
            add(this.form).then(response => {
              this.submitFormLoading = false
              // 关闭弹框，刷新列表
              this.$emit('closeDialogAndRefresh')
            }).catch(() => {
              this.submitFormLoading = false
            })
          }
        } else {
          this.submitFormLoading = false
          return false
        }
      })
    },
    clearValidate() {
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    close() {
      this.submitFormLoading = false
      this.$emit('cancel')
    }
  }
}
</script>
